/**
 * COPYRIGHT. Harry Wu 2011. ALL RIGHTS RESERVED.
 * Project: just
 * Author: Harry Wu <harrywu304@gmail.com>
 * Created On: May 30, 2011 3:52:26 PM
 *
*/ 
package org.justframework.ext.zeroc;

import java.net.URI;
import java.util.Map;

import org.justframework.core.ClientConnector;
import org.justframework.core.Request;
import org.justframework.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import Ice.Communicator;
import Ice.InitializationData;
import Ice.ObjectPrx;
import Ice.Properties;
import Ice.Util;

/**
 * @author whx
 *
 */
public class IceClient implements ClientConnector{
	
	/**
	 * logger
	 */
	private static Logger logger = LoggerFactory.getLogger(IceClient.class);
	
	private static Communicator ic = null;
	
	static {
		//初始化Ice Communicator
		Properties prop = Util.createProperties();
		prop.setProperty("Ice.MessageSizeMax", "1024");
		prop.setProperty("Ice.ThreadPool.Client.Size", "10");
		prop.setProperty("Ice.ThreadPool.Client.SizeMax", "200");
		prop.setProperty("Ice.ThreadPool.Client.SizeWarn", "200");
		//连接超时时间，单位为毫秒，默认为1秒
		prop.setProperty("Ice.Override.ConnectTimeout", "1000");
		//连接的生命周期，单位为秒，默认为60秒
		prop.setProperty("Ice.ACM.Client", "60");
		prop.setProperty("Ice.TCP.RcvSize", "65535");
		prop.setProperty("Ice.TCP.SndSize", "65535");
		InitializationData initData = new InitializationData();
		initData.properties = prop;
		ic = Ice.Util.initialize(initData);	
	}
	
	public static Object get(URI uri, Map params){
		ObjectPrx target = getNativeProxy(":tcp -h "+ uri.getHost() + " -p " + uri.getPort() + " -t 5000", "ZeroRpc");
		ZerocRpcPrx helper = ZerocRpcPrxHelper.checkedCast(target);
		return helper.handle("");
	}
	
	/**
	 * 获取ICE代理对象
	 * @param endpoint -- 服务器端点
	 * @param itfcClazz -- 接口类
	 * @return -- 接口类在服务器上的实现在本地的代理对象
	 */
	private static ObjectPrx getNativeProxy(String endpoint, String servantId) {
		 
		logger.debug("endpoint:{}, servantId:{}", endpoint, servantId);
		ObjectPrx target = ic.stringToProxy(servantId + endpoint);	
		return target; 
	}

	/* (non-Javadoc)
	 * @see org.justframework.core.ClientConnector#rpc(org.justframework.core.Request)
	 */
	public Response rpc(Request request) {
		// TODO Auto-generated method stub
		return null;
	}
	

}
